package cgl.narada.service.security.kmc.messages;

import java.io.IOException;
import java.io.Serializable;

import javax.crypto.SecretKey;

import org.apache.log4j.Logger;

import cgl.narada.service.security.kmc.SecurityToken;
import cgl.narada.util.ObjectMarshaller;

public class SecureTopicKey implements Serializable {

    static Logger log = Logger.getLogger("SecureTopicKey");
    
    /** Topic secret key */
    SecretKey secretKey;
    
    /** Security Token*/
    SecurityToken securityToken;
    
    public SecureTopicKey(SecretKey sec, SecurityToken tok) {
        secretKey = sec;
        securityToken = tok;
    }

    /**
     * Marshalls the object into an array of bytes.
     * 
     * @return an array of bytes representing the marshaller object, null if
     *         error occurs
     */
    public byte[] getBytes() {
       try {
          return ObjectMarshaller.marshall(this);
       } catch (IOException e) {
          log.error("", e);
          return null;
       }
    }

    /**
     * Unmarshalls the bytes as a SecureTopicKey object
     * 
     * @param objectBytes
     * @return SecureTopicKey if successful, null if error occurs
     */
    public static SecureTopicKey createObjectFromBytes(byte[] objectBytes) {
       try {
          return (SecureTopicKey) ObjectMarshaller.unmarshall(objectBytes);
       } catch (IOException e) {
          log.error("", e);
       } catch (ClassNotFoundException e) {
          log.error("", e);
       }
       return null;
    }

    public SecretKey getSecretKey() {
        return secretKey;
    }

    public SecurityToken getSecurityToken() {
        return securityToken;
    }

    
}
